package com.example.datajpatable.webapi;


import antlr.StringUtils;
import com.example.datajpatable.core.PageUtils;
import com.example.datajpatable.domain.Student;
import com.example.datajpatable.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {


    @Autowired
    private StudentService studentService;


    /**
     * 读取的url:/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students = studentService.findAll();

        return students;
    }

    /**
     * 通过名字进行查询/webapi/student/getList
     * @param name
     * @return
     */
    @GetMapping ("/getList/{name}")
    public List<Student> getList(@PathVariable String name){
        List<Student> student1 = studentService.findBySname(name);
        return student1;
    }

    /**
     * 新增学生的方法/webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1 = studentService.insert(student);
        return student1;
    }

    /**
     * 更新学生的方法/webapi/student/update
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student){
        Student student1 = studentService.update(student);
        return student1;
    }
    /**
     * 删除学生的方法/webapi/student/delete
     * @param student
     * @return
     */
//    @DeleteMapping ("/delete")
//    public void delete(Student student){
//        studentService.delete(student);
//
//    }

    /**
     * 读取的url:/webapi/student/get
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        return student;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);
    }

    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue="0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "sname",defaultValue = "") String sname,
                               @RequestParam(value = "sex",defaultValue = "") String sex
                                   ){
        Page<Student> studentPage;
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        if(ObjectUtils.isEmpty(sname)  && ObjectUtils.isEmpty(sex)){
            Pageable pageable = PageRequest.of(page,size, sort);
            studentPage = studentService.findAll(pageable);
        }else if(ObjectUtils.isEmpty(sname) && !ObjectUtils.isEmpty(sex)){
            Student student = new Student();
            student.setSex(sex);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对性别的精确查询
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.exact())
                    ;
            Example<Student> example = Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }else if(!ObjectUtils.isEmpty(sname) && ObjectUtils.isEmpty(sex)){
            Student student = new Student();
            student.setSname(sname);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对姓名的模糊查询
                    .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains())
                    ;
            Example<Student> example = Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }
        else{
            Student student = new Student();
            student.setSname(sname);
            student.setSex(sex);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对姓名的模糊查询
                    .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains())
                    //对性别的精确查询
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.exact())
                    ;
            Example<Student> example = Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }

}
